<template>
  <!-- 通过手机验证身份 -->
  <div class="form">
    <zx-form-group :title="$t('OLD_EMAIL')">
      <zx-input v-model="oldEmail.email" type="text" :place="$t('OLD_EMAIL_REQUIRE')" ref="email" :rules="rules.email" disabled>
        <span class="input-text" slot="text" @click="getCode(oldEmail.email,'email',$t('GET_CODE'),$event,'oldEmailBool')">{{'GET_CODE'|t}}</span>
      </zx-input>
    </zx-form-group>
    <zx-form-group :title="$t('CODE_LABEL')">
      <zx-input v-model="oldEmail.code" :place="$t('CODE_PLACE')" ref="code" :rules="rules.code">
      </zx-input>
    </zx-form-group>
    <zx-form-group>
      <input type="button" class="submit-btn mt-20 green-btn radius" :value="$t('NEXT_STEP')" @click="nextStep('oldEmail')">
    </zx-form-group>
  </div>
</template>
<script>
  import { Form, Func, Auth, Rules } from '@/providers';
  import { User, UserInfo } from '@/services';
  export default {
    name: 'identity-through-email',
    data() {
      return {
        oldEmailBool: false,
        oldEmail: {
          email: Auth.getItem('email'),
          code: ''
        },
        rules: Rules
      };
    },
    methods: {
      // 获取验证码
      getCode(val, type, innerText, event, clickBool) {
        if (this[clickBool]) { return false; };
        if (!Form.validItem(this, type)) { return; };
        this[clickBool] = true;
        if (type == 'phone') {
          Form.SendMobile(val, event.target, clickBool, innerText);
        } else {
          Form.SentMail(val, event.target, clickBool, innerText);
        }
      },
      // 发送手机
      async SendMobile(val, target, clickBool, innerText) {
        try {
          let res = await UserInfo.SendMobile({
            mobile: val
          });
          this.$dialog.toast({
            msg: res.desc,
            position: 'top'
          });
          Func.countdown(target, this, clickBool, innerText);
        } catch (error) {
          console.log(error);
        }
      },
      // 发送邮箱
      async SentMail(val, target, clickBool, innerText) {
        try {
          let res = await UserInfo.SentMail({
            mail: val
          });
          this.$dialog.toast({
            msg: res.desc,
            position: 'top'
          });
          Func.countdown(target, this, clickBool, innerText);
        } catch (error) {
          console.log(error);
        }
      },
      // 下一步
      async nextStep(formName) {
        if (!Form.validataForm(this, formName)) {
          return false;
        };
        let _parmas = this[formName];
        try {
          let res = await User.ChangeEmailValidata({
            token: Auth.getItem('token'),
            code: formName == 'phoneForm' ? _parmas.phone_code : _parmas.code,
            type: formName == 'phoneForm' ? 1 : 2
          });
          this.$emit('nextStep');
        } catch (error) {
          console.log(error);
        }
      }
    }
  };
</script>